package com.wxc.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

/**
 * @author wxc
 * @date 2021年11月23日 19:20
 */
@Configuration
public class CorsConfig {

    /**
     * 配置运行跨域
     */
    @Bean
    public CorsWebFilter corsWebFilter() {
        UrlBasedCorsConfigurationSource corsConfigurationSource = new UrlBasedCorsConfigurationSource();
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        // 允许所有请求头
        corsConfiguration.addAllowedHeader("*");
        // 允许所有请求
        corsConfiguration.addAllowedMethod("*");
        // 允许所有请求来源
        corsConfiguration.addAllowedOrigin("*");
        // 是否允许携带cookie
        corsConfiguration.setAllowCredentials(true);
        corsConfigurationSource.registerCorsConfiguration("/**", corsConfiguration);
        return new CorsWebFilter(corsConfigurationSource);
    }
}
